<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="140px">
    <el-form-item label="品牌名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="品牌名称"></el-input>
    </el-form-item>
    <el-form-item label="图片" prop="listPicUrl">
      <el-input v-model="dataForm.listPicUrl" placeholder="图片"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="simpleDesc">
      <el-input v-model="dataForm.simpleDesc" type="textarea" :rows="2" placeholder="描述"></el-input>
    </el-form-item>
    <el-form-item label="图片" prop="picUrl">
      <el-input v-model="dataForm.picUrl" placeholder="图片"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sortOrder">
      <el-input-number v-model="dataForm.sortOrder" controls-position="right" :min="1"></el-input-number>
    </el-form-item>
    <el-form-item label="显示" prop="isShow">
      <el-radio v-model="dataForm.isShow" label="1">是</el-radio>
      <el-radio v-model="dataForm.isShow" label="0">否</el-radio>
    </el-form-item>
    <el-form-item label="展示价格" prop="floorPrice">
      <el-input-number v-model="dataForm.floorPrice" controls-position="right" :min="1"></el-input-number>
    </el-form-item>
    <el-form-item label="app显示图片" prop="appListPicUrl">
      <el-input v-model="dataForm.appListPicUrl" placeholder="app显示图片"></el-input>
    </el-form-item>
    <el-form-item label="新品牌" prop="isNew">
      <el-radio v-model="dataForm.isNew" label="1">是</el-radio>
      <el-radio v-model="dataForm.isNew" label="0">否</el-radio>
    </el-form-item>
    <el-form-item label="新品牌图片" prop="newPicUrl">
      <el-input v-model="dataForm.newPicUrl" placeholder="图片"></el-input>
    </el-form-item>
    <el-form-item label="新品牌排序" prop="newSortOrder">
      <el-input-number v-model="dataForm.newSortOrder" controls-position="right" :min="1"></el-input-number>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          listPicUrl: '',
          simpleDesc: '',
          picUrl: '',
          sortOrder: '',
          isShow: '1',
          floorPrice: '',
          appListPicUrl: '',
          isNew: '0',
          newPicUrl: '',
          newSortOrder: ''
        },
        dataRule: {
          name: [
            { required: true, message: '品牌名称不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/platform/brand/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.data.name
                this.dataForm.listPicUrl = data.data.listPicUrl
                this.dataForm.simpleDesc = data.data.simpleDesc
                this.dataForm.picUrl = data.data.picUrl
                this.dataForm.sortOrder = data.data.sortOrder
                this.dataForm.isShow = data.data.isShow + ''
                this.dataForm.floorPrice = data.data.floorPrice
                this.dataForm.appListPicUrl = data.data.appListPicUrl
                this.dataForm.isNew = data.data.isNew + ''
                this.dataForm.newPicUrl = data.data.newPicUrl
                this.dataForm.newSortOrder = data.data.newSortOrder
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/platform/brand/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'listPicUrl': this.dataForm.listPicUrl,
                'simpleDesc': this.dataForm.simpleDesc,
                'picUrl': this.dataForm.picUrl,
                'sortOrder': this.dataForm.sortOrder,
                'isShow': this.dataForm.isShow,
                'floorPrice': this.dataForm.floorPrice,
                'appListPicUrl': this.dataForm.appListPicUrl,
                'isNew': this.dataForm.isNew,
                'newPicUrl': this.dataForm.newPicUrl,
                'newSortOrder': this.dataForm.newSortOrder
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
